//
//  BGMyTenderController.m
//  bigMall
//
//  Created by ios on 16/9/5.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGMyTenderController.h"
#import "BGMyTenderCell.h"
#import "BGDateResult.h"
#import "BGOwnBidLogModel.h"
#import "BGCancelBidModel.h"
#import "BGTenderInfoViewController.h"

@interface BGMyTenderController () <UITableViewDelegate,UITableViewDataSource> {
    
    UITableView *_tableView;
    NSMutableArray *_dataArr;
    int index;
    UIImageView *imageView;
    UILabel *lable;
    
}

@end

@implementation BGMyTenderController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的投标";
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    
    [self createUI];
    [self initDat];
    
}
- (void)initDat {
    
    _dataArr = [NSMutableArray array];
    
}
- (void)createUI {
    
    _tableView =[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.showsVerticalScrollIndicator = NO;
//    _tableView.editing = YES;
//    _tableView.allowsMultipleSelectionDuringEditing = YES;
//    _tableView.allowsSelectionDuringEditing = YES;
    [self.view addSubview:_tableView];
    
    //刷新的加载
    _tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(index)];
    
    [_tableView.header beginRefreshing];
    
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake((BGWidth - 70 ) / 2 + 10, (_tableView.height - 109 - 160) / 3, 70, 90)];
    imageView.image = [UIImage imageNamed:@"抱歉~暂无数据"];
    
    lable = [[UILabel alloc] initWithFrame:CGRectMake((BGWidth - 120 ) / 2 + 10, imageView.bottom + 21, 120, 16)];
    lable.text = @"抱歉~暂无数据";
    lable.font = [UIFont systemFontOfSize:16];
    [_tableView addSubview:lable];
    
    [_tableView addSubview:imageView];
    lable.hidden = YES;
    imageView.hidden = YES;
    
}
- (void)addIndex {
    
    index++;
//    [_dataArr removeAllObjects];
    [self loadInter];
    
}
- (void)index {
    
    index = 1;
    [_dataArr removeAllObjects];
    [self loadInter];
    
}

- (void)loadInter {
    
    [BGDateResult ownBidLogWithThisPage:[NSString stringWithFormat:@"%d",index] pageNum:@"10" success:^(NSObject *result) {
        
        BGOwnBidLogModel *model = [[BGOwnBidLogModel alloc] init];
        model = (BGOwnBidLogModel *)result;
                NSArray *listArr = [NSArray arrayWithArray:model.list];
//        NSMutableArray *arr = [NSMutableArray array];
                for (BGOwnBidLogValueModel *model in listArr) {
                    [_dataArr addObject:model];
                }
//                       _dataArr = arr;
        
        //                       _dataArr = arr;
        if (_dataArr.count == 0 && index == 1) {
            
            [_tableView.header endRefreshing];
            [_tableView.footer endRefreshingWithNoMoreData];
            
            //判断数据源有无
            
            lable.hidden = NO;
            imageView.hidden = NO;
            
            
        }
        if ([model.allPage intValue] > index) {
            _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(addIndex)];
        }
        if (listArr.count == 0) {
            [_tableView.footer endRefreshingWithNoMoreData];
            return;
        }
        lable.hidden = YES;
        imageView.hidden = YES;                [_tableView.footer endRefreshing];
                [_tableView.header endRefreshing];
                [_tableView reloadData];
                

        
    } failure:^(NSError *error) {
        
    }];
    
}

#pragma mark - delegate -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 125;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView registerNib:[UINib nibWithNibName:@"BGMyTenderCell" bundle:nil] forCellReuseIdentifier:@"celId"];
    BGMyTenderCell *cel = [tableView dequeueReusableCellWithIdentifier:@"celId" forIndexPath:indexPath];
    BGOwnBidLogValueModel *model = [[BGOwnBidLogValueModel alloc] init];
    if (_dataArr != 0) {
        
        model = _dataArr[indexPath.row];
        cel.bianhao.text = model.title;
        cel.toubiaojiage.text = model.price;
        cel.toubiaozongliang.text = model.amount;
        cel.zhuangtai.text = model.result_str;
        NSDate *dat = [NSDate dateWithTimeIntervalSince1970:[model.add_time intValue]];
        NSDateFormatter*df = [[NSDateFormatter alloc]init];
        [df setDateFormat:@"yyyy-MM-dd"];
        NSString *str = [df stringFromDate:dat];
        cel.toubiaoshijian.text =  str;
        
    }
    return cel;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    BGOwnBidLogValueModel *model = [[BGOwnBidLogValueModel alloc] init];
    if (_dataArr != 0) {
        
        model = _dataArr[indexPath.row];
        
    }
    
    if ([model.status isEqualToString:@"1"]) {
        BGTenderInfoViewController *a = [[BGTenderInfoViewController alloc] init];
        
        NSLog(@"model.idStr === %@",model.idStr);
        
        a.goodId = model.invite_id;
        
        self.hidesBottomBarWhenPushed = YES;
        
        [self.navigationController pushViewController:a animated:YES];
    } else {
        [self showMessage:@"此招标已不存在"];
    }
    
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
//编辑的样式
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BGOwnBidLogValueModel *model = [[BGOwnBidLogValueModel alloc] init];
    if (_dataArr != 0) {
        
        model = _dataArr[indexPath.row];
        
    }
    
    // 添加一个的撤销按钮
    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"撤销" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"点击了删除");
        
        // 1. 更新数据
        BGOwnBidLogValueModel *model = [[BGOwnBidLogValueModel alloc] init];
        if (_dataArr != 0) {
            
            model = _dataArr[indexPath.row];
            
        }
        
        [BGDateResult cancelBidWithIdstr:model.idStr success:^(NSObject *result) {
            
            BGCancelBidModel *a = [[BGCancelBidModel alloc] init];
            a = (BGCancelBidModel *)result;
            if ([a.status isEqualToString:@"1"]) {
                [self showMessage:[NSString stringWithFormat:@"%@，请刷新页面",a.msg]];
            }else {
                [self showMessage:a.msg];
            }
            
        } failure:^(NSError *error) {
            
        }];

        // 1. 更新数据
//        NSArray *indexPaths = @[indexPath]; // 构建 索引处的行数 的数组
//        
//        [_dataArr removeObjectAtIndex:indexPath.row];
//        
//        [tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:(UITableViewRowAnimationLeft)];
        // 2. 更新UI
        //        [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];
    }];
    
    // 删除一个置顶按钮
    UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"点击了置顶");
        
        
        // 2. 更新UI
        NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
        [tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];
    }];
    topRowAction.backgroundColor = [UIColor blueColor];
    
    // 添加一个更多按钮
    UITableViewRowAction *moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"更多"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"点击了更多");
        
        [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
    }];
    moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
    
    // 将设置好的按钮放到数组中返回
        return @[deleteRowAction];
    
}
- (void)showMessage:(NSString *)message {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    
    [alertController addAction:okAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
